package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Gys;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-3-21
 * Time: 下午2:19
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class GysDao extends SimpleHibernateTemplate<Gys, Long> {
    public Page<Gys> findGysList(Long nbgsId, Long glqyId, String name, int pageNo, int pageSize) {
        Page<Gys> page = new Page<Gys>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        List<String> conditions = new ArrayList<String>();
        List<Object> params = new ArrayList<Object>();
        if (glqyId != null) {
            conditions.add(" glqy.id=? ");
            params.add(glqyId);
        }
        if (nbgsId != null) {
            conditions.add(" nbgs.id=? ");
            params.add(nbgsId);
        }
        if (StringUtils.isNotEmpty(name)) {
            conditions.add(" concat(glqy.qyfr.qyqc,glqy.qyfr.qyjc) like ? ");
            params.add("%" + name + "%");
        }
        String hql = "from Gys " + (conditions.isEmpty() ? "" : " where " + StringUtils.join(conditions.toArray(), " and "));
        return this.find(page, hql, params.toArray());
    }

    /**
     * 方法名:     checkIsOnly
     * 功能描述:   判断参数名称是否重复
     * 输入参数:   @param id 参数的ID propertyName 参数名称
     * 返回值:     @return boolean true表示数据库中已存在，false表示数据库中不存在
     */
    public Boolean checkIsOnly(Long id, Object propertyName) {
        String hql;
        if (id == null) {
            //修改检验
            hql = "from Gys where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyName);
        } else {
            //新增检验
            hql = "from Gys where " + propertyName + " = ? and id <>?";
            return this.existsQuery(hql, propertyName, id);
        }
    }

}
